package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.pojo.Category;
import com.zhentao.service.CategoryService;
import com.zhentao.mapper.CategoryMapper;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* @author ls
* @description 针对表【category(商品分类表)】的数据库操作Service实现
* @createDate 2025-07-02 09:33:46
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Override
    public Integer getMaxSortByParentId(Long parentId) {
        QueryWrapper<Category> query = new QueryWrapper<>();
        query.eq("parent_id", parentId)
                .select("IFNULL(MAX(sort), 0) as maxSort");
        Map<String, Object> map = getMap(query);
        return map != null ? ((Number)map.get("maxSort")).intValue() : null;
    }

    @Override
    public Integer getChildCount(Long parentId) {
        QueryWrapper<Category> query = new QueryWrapper<>();
        query.eq("parent_id", parentId)
                .eq("is_deleted", 0);
        return Math.toIntExact(count(query));
    }
    @Override
    public Integer getPrevSort(Long parentId, Integer sort) {
        if (parentId == null || sort == null) {
            return null;
        }

        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sort")
                .eq("parent_id", parentId)
                .lt("sort", sort)
                .eq("is_deleted", 0)
                .orderByDesc("sort")
                .last("LIMIT 1");

        Category category = getOne(queryWrapper);
        return category != null ? category.getSort() : null;
    }

    @Override
    public Integer getNextSort(Long parentId, Integer sort) {
        if (parentId == null || sort == null) {
            return null;
        }

        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sort")
                .eq("parent_id", parentId)
                .gt("sort", sort)
                .eq("is_deleted", 0)
                .orderByAsc("sort")
                .last("LIMIT 1");

        Category category = getOne(queryWrapper);
        return category != null ? category.getSort() : null;
    }

    @Override
    public boolean isDescendant(Long parentId, Long categoryId) {
        // 防止空指针和循环引用
        if (parentId == null || categoryId == null || parentId.equals(0L)) {
            return false;
        }

        // 使用Set记录已访问的父级ID，防止循环引用导致的无限循环
        Set<Long> visitedParentIds = new HashSet<>();
        Long currentParentId = parentId;

        while (currentParentId != null && !currentParentId.equals(0L)) {
            // 检查是否形成循环
            if (visitedParentIds.contains(currentParentId)) {
                return false;
            }
            visitedParentIds.add(currentParentId);

            // 找到目标分类
            if (currentParentId.equals(categoryId)) {
                return true;
            }

            // 获取当前父分类
            Category parent = getById(currentParentId);
            if (parent == null || parent.getIsDeleted() == 1) {
                break;
            }

            // 向上查找
            currentParentId = parent.getParentId();
        }

        return false;
    }
}




